﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WinnerCMS.WebUI.Admin.Explorer.Index" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>资源管理器</title>
    <%: Scripts.Render("~/Scripts/AdminBase") %>
    <%: Styles.Render("~/Content/Bootstrap/css") %>
    
    <script>
        $(function () {
            FileList("");
            $('#tt').tree({
                url: '/api/Explorer/Folders',
                method: "post",
                idField: 'CustomField1',
                animate: true,
                dnd: true,
                onClick: function (node) {
                    FileList(node.id);
                },
                onContextMenu: function (e, node) {
                    e.preventDefault();
                    $(this).tree('select', node.target);
                    $('#mmFolders').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                },
                onLoadSuccess: function (node, data) {
                    //$('#tt').tree("collapseAll");
                    //$('#tt').tree("expand", node.target);
                }
            });
        });
        function upload() {
            $("#dialog-modal").dialog({
                width: 540,
                height: 350,
                title: "文件上传",
                modal: true
            });
            var node = $('#tt').tree('getSelected');
            var urlC = $("#dialog-modal-url");
            urlC.attr("src", "../window/UpLoad.aspx?dir=" + escape(node.id));
            urlC.width(500);
            urlC.height(300);
        }
        function FileList(path) {
            $('#t_List').datagrid({
                url: '/api/Explorer/GetFileList',
                method: "post",
                queryParams: {
                    path: path
                },
                pageSize: ConfigPageSize(),
                pageList: ConfigPageList(),
                onLoadError: function (xmlHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                },
                onLoadSuccess: function (data) {
                    //alert( data.total);
                }
            });
        }
        function Dateformatter(value, rowData, rowIndex) {
            try {
                var date = new Date(value);
                return date.format("yyyy-MM-dd hh:mm:ss");
            } catch (e) {
                return value;
            }
        }
        function Nameformatter(value, rowData, rowIndex) {
            var html = '<div style="float: left;"><img src="../Images/FileICO/' + rowData.Extension + '.gif" onerror="this.src=\'../Images/FileICO/err.gif\'" alt="" /> <a href="FileEditor.aspx?dir=' +  escape( rowData.FullName) + '" target="_blank" title=\'文件大小：0\'>' + rowData.Name + '</a><a href="DownloadFile.aspx?dir=' + escape(rowData.FullName )+ '" target="_blank"> <img src="../Images/FileICO/download.png" alt="" /></a></div>';

            if (rowData.Extension == "rar") {
                html += '<div style="float: right;"> <input class="btn btn-default" type="button" value="解压"></div>';
            }
            return html;
        }
        function ReNameformatter(value, rowData, rowIndex) {
            return "<a href=\"javascript:void(0)\" onclick=\"ReNameHandler('" + escape(value) + "')\">移动/重命名</a>";
        }
        function Deleteformatter(value, rowData, rowIndex) {
            return "<a href=\"javascript:void(0)\" onclick=\"DeleteHandler('" + escape(value) + "')\">删除</a>";
        }
        //删除文件
        function DeleteHandler(path) {
            $.messager.confirm('Confirm', '确认删除？', function(r) {
                if (r) {
                    $.ajax({
                        type: "Delete",
                        url: "/api/Explorer/DeleteFile?path=" + path,
                        success: function(data) {
                            if (data.errors.length > 0) {
                                alert(data.errors);
                            } else {
                                $('#t_List').datagrid('reload');
                                alert("删除成功。");
                            }
                        }
                    });
                }
            });
        }

        function deleteFolder() {
            $.messager.confirm('Confirm', '确认删除？', function(r) {
                if (r) {
                    var node = $('#tt').tree('getSelected');
                    $.ajax({
                        type: "Delete",
                        url: "/api/Explorer/DeleteDirectory?path=" + escape(node.id),
                        success: function(data) {
                            if (data.errors.length > 0) {
                                alert(data.errors);
                            } else {
                                $('#tt').tree('reload');
                            }
                        }
                    });
                }
            });
        }

        function newFolder() {
            var node = $('#tt').tree('getSelected');
            $.messager.prompt('新建文件夹', '名称：', function (r) {
                if (r) {
                    $.ajax({
                        type: "post",
                        url: "/api/Explorer/NewFolder?path=" + node.id + "&name=" +escape(r),
                        success: function (data) {
                            if (data.errors.length > 0) {
                                alert(data.errors);
                            } else {
                                $('#tt').tree('reload');
                                $('#dlgNewFolder').dialog("close");
                            }
                        }
                    });
                }
            });
        }
        //重命名文件夹
        function renameFolder() {
            var node = $('#tt').tree('getSelected');
            var prompt = $.messager.prompt('重命名', '新名称：', function (r) {
                if (r) {
                    $.ajax({
                        type: "post",
                        url: "/api/Explorer/RenameFolder?sourcePath=" + node.id + "&newname=" + escape(r),
                        success: function (data) {
                            if (data.errors.length > 0) {
                                alert(data.errors);
                            } else {
                                $('#tt').tree('reload');
                                $('#dlgNewFolder').dialog("close");
                            }
                        }
                    });
                }
            });
     
            prompt.prevObject.find(".messager-input").val(node.text);
        }
    </script>
    <style>
        .pagination {
            display: block;
        }

        #dlgNewFolder .panel {
            margin-bottom: 0;
        }
    </style>
</head>
<body class="easyui-layout">
    <div region="west" border="false" style="width: 200px;">
        <ul id="tt"></ul>
    </div>
    <div region="center" style="overflow: hidden;">
        <table id="t_List" style="width: 100%; height: auto" singleselect="false" idfield="FullName"
            fit="true" pagination="true" nowrap="true" striped="true" collapsible="true"
            remotesort="true" sortname="LastWriteTime" sortorder="desc">
            <thead>
                <tr>
                    <th field="" width="40" checkbox="true" align="center"></th>
                    <th field="Name" formatter="Nameformatter" width="360" sortable="true" align="left">名称</th>
                    <th field="FullName" formatter="ReNameformatter" width="120" align="center">移动/重命名</th>
                    <th field="DelFullName" formatter="Deleteformatter" width="80" align="center">删除</th>
                    <th field="LastWriteTime" width="136" sortable="true" formatter="Dateformatter" align="center">最后修改</th>

                </tr>
            </thead>
        </table>
    </div>
    <div id="mmFolders" class="easyui-menu" style="width: 120px;">
        <div onclick="newFolder()" data-options="iconCls:'icon-add'">新建文件夹</div>
        <div onclick="renameFolder()">重命名</div>
        <div class="menu-sep"></div>
        <div onclick="deleteFolder()" data-options="iconCls:'icon-remove'">删除</div>
<div onclick="upload()">上传文件</div>
    </div>
 <div id="dialog-modal">
        <iframe id="dialog-modal-url" frameborder="0" scrolling="no"></iframe>
    </div>
</body>
</html>
